﻿using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Ozone.Models;
using Ozone.Views.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.SystemCenter.AdvancedParameter
{
    /// <summary>
    /// UCSystemCenter_Advanced.xaml 的交互逻辑
    /// </summary>


    public partial class UCSystemCenter_Advanced : UserControl
    {

        int nLineNumb = 0;
        int nListIndex = 0;
        AdvParam advParamA;
        AdvParam advParamB;
        public UCSystemCenter_Advanced()
        {
            InitializeComponent();

            advParamA = BLL.AdvParamBLL.get("A");
            advParamB = BLL.AdvParamBLL.get("B");
            tbiA.DataContext = advParamA;
            tbiB.DataContext = advParamB;

            Login(false);
        }

        public void setLineNum(int LineNumb)
        {
            nLineNumb = LineNumb;
            nListIndex = LineNumb - 1;

            tbiA.DataContext = advParamA;
            tbiB.DataContext = advParamB;
            // DataContext = ParamCenter.Entity.lstAdvParam[nListIndex];

        }

        //稳流阀设置按钮
        private void btnWYF_Click(object sender, RoutedEventArgs e)
        {
            string flag = "A";
            if (tabMain.SelectedIndex != 0)
                flag = "B";
            string tag = ((Button)sender).Tag.ToString();

        }

        //背压阀按钮事件
        private void btnBYF_Click(object sender, RoutedEventArgs e)
        {
            string flag = "A";
            if (tabMain.SelectedIndex != 0)
                flag = "B";

            string tag = ((Button)sender).Tag.ToString();

        }

        //高压恒流输液泵设置
        private void btnHSYB_Click(object sender, RoutedEventArgs e)
        {
            string flag = "A";
            if (tabMain.SelectedIndex != 0)
                flag = "B";
            string tag = ((Button)sender).Tag.ToString();

        }


        //private void setParamEvent(string tag, Grid grd,string ABFlag="A")
        //{

        //    if (!ConfirmOper(tag))
        //        return;


        //    string val = "";
        //    TextBox txt = Tools.GetChildObjectByTag<TextBox>(grd, tag);
        //    if (txt != null)
        //        val = txt.Text;

        //    bool bRet = DeviceCenter.Entity.lstDevS10[nListIndex].setParam(tag, val, ABFlag);

        //    bRet = true;
        //    if (bRet)
        //    {
        //        Message.Success("执行成功", 1000, true);
        //        if(ABFlag == "A")
        //            BLL.AdvParamBLL.save(advParamA, ABFlag);
        //        else
        //            BLL.AdvParamBLL.save(advParamB, ABFlag);
        //    }
        //    else
        //    {
        //        Message.Warning("执行失败", 1000, true);
        //    }

        //}



        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();

            if (tag == "login")
            {

            }

            if (tag == "logout")
            {
                Login(false);
            }

        }

        private bool ConfirmOper(string tag)
        {
            string content = "";

            if (tag == "WYF12")
                content = "确认液位高度清零吗?";

            if (tag == "HJYF06")
                content = "确认压力清零吗?";

            if (tag == "HJYF03")
                content = "确认擦除标定参数吗?";

            if (tag == "HJYF05")
                content = "确认重新标定参数吗?";

            if (tag == "HJYF06")
                content = "确认压力清零吗?";


            if (tag == "NJYF03")
                content = "确认擦除标定参数吗?";

            if (tag == "NJYF05")
                content = "确认重新标定参数吗?";

            if (tag == "BYF06")
                content = "确认压力清零吗?";

            if (tag == "BYF03")
                content = "确认擦除标定参数吗?";

            if (tag == "BYF05")
                content = "确认重新标定参数吗?";

            if (tag == "HSYB04")
                content = "确认压力清零吗?";

            if (tag == "HSYB05")
                content = "确认校正复位吗？";

            if (content != "")
            {
                if (MessageBox.Show(content, "提示",
                            MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                    return false;
            }

            return true;
        }

        private void Login(bool bLogin)
        {
            if (bLogin)
            {
                tabMain.Visibility = Visibility.Visible;

            }
            else
            {
                tabMain.Visibility = Visibility.Visible;

            }
        }

        List<DataHisItem> lstHisData;
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {

            /* WindowSelDate windowSel = new WindowSelDate();
             windowSel.ShowDialog();
             if (!windowSel.bIsOK)
                 return;

             DataHisBLL bll = new DataHisBLL();
             lstHisData = bll.getList(windowSel.dt1.ToString("yyyy-MM-dd HH:mm:ss"),
                                                             windowSel.dt2.ToString("yyyy-MM-dd HH:mm:ss"),
                                                             nLineNumb
                                                             );

             string filepath = "";
             SaveFileDialog dlg = new SaveFileDialog();
             dlg.Title = "保存EXCEL文件";
             dlg.FileName = nLineNumb + "通道数据" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; // Default file name
             dlg.DefaultExt = ".xls"; // Default file extension
             dlg.Filter = "Excel文件|*.xls"; // Filter files by extension
                                           //dlg.InitialDirectory = initFolder;

             if (dlg.ShowDialog() == true)
             {
                 filepath = dlg.FileName;
             }
             else
             {
                 return;
             }


             _ = WindowWaiting.ShowWaitDlgAsync(this, true, "正在生成EXCEL文件，请稍候...");


             _ = toExcelAsync(nListIndex, filepath);
            */
        }


        private async Task toExcelAsync(int nLineNum, string fileName)
        {

            await Task.Run(() =>
            {
                this.Dispatcher.Invoke(delegate ()
                {

                    try
                    {
                        saveToExce1Pro(nLineNum, fileName);//加载数据，比如查询数据库什么的
                        _ = WindowWaiting.ShowWaitDlgAsync(this, false);
                    }
                    catch (Exception)
                    {

                    }
                });
            });

        }


        //快速导出
        public void saveToExce1Pro(int nLineNum, string FilePath)
        {

            try
            {
                // 创建Excel表格
                IWorkbook workbook = new XSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("Sheet1");
                // 写入多行数据
                IRow rowHeader = sheet.CreateRow(0);
                rowHeader.CreateCell(0).SetCellValue("序号");
                rowHeader.CreateCell(1).SetCellValue("时间");
                rowHeader.CreateCell(2).SetCellValue("预热器温度");
                rowHeader.CreateCell(3).SetCellValue("反应柱温度");
                rowHeader.CreateCell(4).SetCellValue("背压阀");
                rowHeader.CreateCell(5).SetCellValue("氢气流速");
                rowHeader.CreateCell(6).SetCellValue("氮气流速");
                rowHeader.CreateCell(7).SetCellValue("泵流速");
                rowHeader.CreateCell(8).SetCellValue("泵压力");
                rowHeader.CreateCell(9).SetCellValue("液位");


                int nCount = lstHisData.Count;
                //填充数据
                for (int i = 0; i < nCount; i++)
                {
                    DataHisContent dp = JsonConvert.DeserializeObject<DataHisContent>(lstHisData[i].FJson);

                    if (dp == null)
                        continue;

                    IRow row = sheet.CreateRow(i + 1);
                    row.CreateCell(0).SetCellValue(i + 1);
                    row.CreateCell(1).SetCellValue(lstHisData[i].FDate.ToString());
                    row.CreateCell(2).SetCellValue(dp.PreheaterTemp);
                    row.CreateCell(3).SetCellValue(dp.ReactorTemp1);
                    row.CreateCell(4).SetCellValue(dp.BPPress);
                    row.CreateCell(5).SetCellValue(dp.HydrogenFlow);
                    row.CreateCell(6).SetCellValue(dp.NitrogenFlow);
                    row.CreateCell(7).SetCellValue(dp.InfusionPumpFlow);
                    row.CreateCell(8).SetCellValue(dp.InfusionPumpPress);
                    row.CreateCell(9).SetCellValue(dp.LiquidLevel);
                }

                // 保存Excel文件
                FileStream fileStream = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
                workbook.Write(fileStream);
                fileStream.Close();

                MessageBox.Show("导出成功!", "提示",
                         MessageBoxButton.OK, MessageBoxImage.Information);

            }

            catch (Exception e)
            {
             

            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {

        }
    }
}
